package com.engine.salary.entity.salaryacct.bo;

import com.engine.salary.entity.datacollection.DataCollectionEmployee;
import com.engine.salary.entity.salaryacct.po.SalaryAcctEmployeePO;
import com.engine.salary.entity.salaryformula.dto.SalaryFormulaEmployeeDTO;
import com.engine.salary.entity.salaryitem.po.SalaryItemPO;
import com.engine.salary.entity.salarysob.po.SalarySobBackItemPO;
import com.engine.salary.entity.salarysob.po.SalarySobItemPO;
import com.engine.salary.enums.UserStatusEnum;
import com.engine.salary.enums.salaryitem.SalaryDataTypeEnum;
import com.engine.salary.util.JsonUtil;
import com.engine.salary.util.SalaryEntityUtil;
import com.engine.salary.util.SalaryI18nUtil;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import weaver.general.Util;

import java.math.BigDecimal;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.Optional;

/**
 * 薪资核算公式变量
 * <p>Copyright: Copyright (c) 2022</p>
 * <p>Company: 泛微软件</p>
 *
 * @author qiantao
 * @version 1.0
 **/
public class SalaryAcctFormulaBO {

    /**
     * 对核算结果做舍入操作
     *
     * @param value
     * @param salaryItem
     * @param salarySobBackItems                薪资账套回算项目List
     * @param salarySobBackItemMap              薪资账套回算项目Map
     * @param salaryItemIdKeySalarySobItemPOMap 薪资账套下薪资项目Map
     * @return
     */
    public static String roundResultValue(String value, SalaryItemPO salaryItem,
                                          List<SalarySobBackItemPO> salarySobBackItems,
                                          Map<Long, SalarySobBackItemPO> salarySobBackItemMap,
                                          Map<Long, SalarySobItemPO> salaryItemIdKeySalarySobItemPOMap) {
        // 值为空，不需要四舍五入
        if (StringUtils.isEmpty(value) || salaryItem == null) {
            return StringUtils.EMPTY;
        }
        // 薪资项目字段类型为string，无需四舍五入
        SalaryDataTypeEnum dataTypeEnum = SalaryDataTypeEnum.parseByValue(salaryItem.getDataType());
        if (dataTypeEnum == SalaryDataTypeEnum.STRING) {
            return value;
        }
        // 获取薪资账套副本中的舍入规则和保留位数，拿不到再去薪资项目中取
        Integer salaryItemRoundingMode = Optional.ofNullable(salaryItemIdKeySalarySobItemPOMap.get(salaryItem.getId())).map(SalarySobItemPO::getRoundingMode)
                .orElse(salaryItem.getRoundingMode());
        Integer salaryItemPattern = Optional.ofNullable(salaryItemIdKeySalarySobItemPOMap.get(salaryItem.getId())).map(SalarySobItemPO::getPattern)
                .orElse(salaryItem.getPattern());
        if (CollectionUtils.isNotEmpty(salarySobBackItems) && salarySobBackItemMap.containsKey(salaryItem.getId())) {
            // 薪资项目是回算项目
            salaryItemRoundingMode = salarySobBackItemMap.get(salaryItem.getId()).getRoundingMode();
            salaryItemPattern = salarySobBackItemMap.get(salaryItem.getId()).getPattern();
        }
        BigDecimal bigDecimalValue = SalaryEntityUtil.empty2Zero(value);
        return SalaryEntityUtil.carryRule(salaryItemPattern, salaryItemRoundingMode, bigDecimalValue).toPlainString();
    }

    /**
     * 变量中的人员信息
     *
     * @param simpleEmployee
     * @return
     */
    public static Map<String, String> convert2FormulaEmployee(DataCollectionEmployee simpleEmployee, SalaryAcctEmployeePO salaryAcctEmployeePO, boolean dynamicEmpInfo) {
        if (simpleEmployee == null) {
            return Collections.emptyMap();
        }
        String sexName = Optional.ofNullable(simpleEmployee.getSex())
                .map(sex -> StringUtils.equals(sex, "0") ? SalaryI18nUtil.getI18nLabel(102440, "男")
                        : SalaryI18nUtil.getI18nLabel(102442, "女"))
                .orElse(StringUtils.EMPTY);

        SalaryFormulaEmployeeDTO formulaEmployee = null;
        if (dynamicEmpInfo) {
            formulaEmployee = SalaryFormulaEmployeeDTO.builder()
                    .employeeId(simpleEmployee.getEmployeeId())
                    .username(simpleEmployee.getUsername())
                    .email(simpleEmployee.getEmail())
                    .mobile(simpleEmployee.getMobile())
                    .telephone(simpleEmployee.getTelephone())
                    .sex(sexName)
                    .status(simpleEmployee.getStatus())
                    .statusName(simpleEmployee.getStatusName())
                    .accountType(simpleEmployee.getAccountType())
                    .accountTypeName(simpleEmployee.getAccountTypeName())
                    .departmentName(simpleEmployee.getDepartmentName())
                    .departmentId(simpleEmployee.getDepartmentId())
                    .subcompanyName(simpleEmployee.getSubcompanyName())
                    .subcompanyId(simpleEmployee.getSubcompanyid())
                    .jobtitleName(simpleEmployee.getJobtitleName())
                    .jobtitleId(simpleEmployee.getJobtitleId())
                    .jobcall(simpleEmployee.getJobcall())
                    .jobcallId(simpleEmployee.getJobcallId())
                    .companystartdate(simpleEmployee.getCompanystartdate())
                    .birthday(simpleEmployee.getBirthday())
                    .workcode(simpleEmployee.getWorkcode())
                    .idNo(simpleEmployee.getIdNo())
                    .build();
        } else {
            formulaEmployee = SalaryFormulaEmployeeDTO.builder()
                    .employeeId(simpleEmployee.getEmployeeId())
                    .username(simpleEmployee.getUsername())
                    .email(simpleEmployee.getEmail())
                    .mobile(simpleEmployee.getMobile())
                    .telephone(simpleEmployee.getTelephone())
                    .sex(sexName)
                    .status(salaryAcctEmployeePO.getStatus())
                    .statusName(UserStatusEnum.getDefaultLabelByValue(new Integer(Util.null2s(salaryAcctEmployeePO.getStatus(), "1"))))
                    .accountType(simpleEmployee.getAccountType())
                    .accountTypeName(simpleEmployee.getAccountTypeName())
                    .departmentName(salaryAcctEmployeePO.getDepartmentName())
                    .departmentId(salaryAcctEmployeePO.getDepartmentId())
                    .subcompanyName(salaryAcctEmployeePO.getSubcompanyName())
                    .subcompanyId(salaryAcctEmployeePO.getSubcompanyId())
                    .jobtitleName(salaryAcctEmployeePO.getJobtitleName())
                    .jobtitleId(salaryAcctEmployeePO.getJobtitleId())
                    .jobcall(salaryAcctEmployeePO.getJobcall())
                    .jobcallId(salaryAcctEmployeePO.getJobcallId())
                    .companystartdate(simpleEmployee.getCompanystartdate())
                    .birthday(simpleEmployee.getBirthday())
                    .workcode(simpleEmployee.getWorkcode())
                    .idNo(simpleEmployee.getIdNo())
                    .build();
        }
        return JsonUtil.parseMap(JsonUtil.toJsonString(formulaEmployee), String.class);
    }
}
